package com.jiuyin.dianjing.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.Toast;

import com.jiuyin.dianjing.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.lang.ref.WeakReference;

public class ShareUtil {
    private static final String UMENG_SOCIALIZE_COPY = "umeng_socialize_copy";
    private static final String UMENG_SOCIALIZE_COPYURL = "umeng_socialize_copyurl";
    static ShareUtil instance;
    public ShareAction shareAction;
    Activity mActivity;
    private String mCopyText;
    private String mCopyLink;
    //资讯链接
    private String mUrl;
    //资讯Title
    private String mTitle;
    private Bitmap mSharedBitmap;
    private String mImageUrl;
    private UMShareListener mShareListener;

    private ShareUtil() {

    }

    public static ShareUtil getInstance(Activity activity, String url, String title) {
        if (instance == null) {
            instance = new ShareUtil();
        }
        instance.mActivity = activity;
        instance.mUrl = url;
        instance.mTitle = title;
        return instance;
    }

    /**
     * 初始化分享
     */
    public void initShare() {
        mCopyText = mActivity.getString(R.string.copy_text);
        mCopyLink = mActivity.getString(R.string.copy_link);
        mShareListener = new CustomShareListener(mActivity);
        shareAction = new ShareAction(mActivity).setDisplayList(
                SHARE_MEDIA.WEIXIN,
                SHARE_MEDIA.SINA,
                SHARE_MEDIA.WEIXIN_CIRCLE)
                .addButton(mCopyLink, mCopyLink, UMENG_SOCIALIZE_COPYURL, UMENG_SOCIALIZE_COPYURL)//addButton:自定义按钮,s:显示的名字,s1:平台名称,s2:按钮图标的图片名字,s3:置灰情况下按钮图标的图片名字
                .setShareboardclickCallback((snsPlatform, share_media) -> {
                    if (snsPlatform.mShowWord.equals(mCopyText) || snsPlatform.mShowWord.equals(mCopyLink)) {
                        ClipBoardUtil.copy(mActivity, mUrl);
                        ToastUtil.showShort(R.string.copy_success);
                    } else {
                        UMWeb web = new UMWeb(mUrl);//分享链接
                        web.setDescription(mUrl);
                        if (!TextUtils.isEmpty(mTitle)) {
                            web.setTitle(mTitle);
                        }
                        if (null != mSharedBitmap) {
                            web.setThumb(new UMImage(mActivity, mSharedBitmap));
                        } else if (mImageUrl != null) {
                            web.setThumb(new UMImage(mActivity, mImageUrl));
                        }
                        new ShareAction(mActivity).withText(mTitle).withMedia(web)
                                .setPlatform(share_media)
                                .setCallback(mShareListener)
                                .share();
                    }
                });

    }

    public void open() {
        if (null != shareAction) {
            shareAction.open();
        }
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String mUrl) {
        this.mUrl = mUrl;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public Bitmap getSharedBitmap() {
        return mSharedBitmap;
    }

    public void setSharedBitmap(Bitmap sharedBitmap) {
        this.mSharedBitmap = sharedBitmap;
    }

    public String getImageUrl() {
        return mImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.mImageUrl = imageUrl;
    }

    public class CustomShareListener implements UMShareListener {
        private WeakReference<Activity> mActivity;

        private CustomShareListener(Activity activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST
                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST
                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享失败啦:" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }
}
